// pages/restpassword/resetpassword.js
let httptool = require("../../utils/zry.httptool.js");
var md5 = require('../../utils/md5.js');
var base64 = require('../../utils/base64.js');  
let app = getApp(); 
Page({
  data:{
    customer:{},
    Tel:"",
    Code:'',/*短信验证码*/
    confirmPwd:'',/*确认密码*/
    codeBtnText:'获取验证码',
    msgprop:'',//消息提示
  },
  telChanage:function(e){
    this.data.customer.Tel = e.detail.value;
    let Tel = e.detail.value;
    this.setData({ customer: this.data.customer,Tel:Tel});
  },
  valCode:function(e){
      let Code=e.detail.value;
      this.setData({Code: Code});
  },
  pwdCheck:function(e){
    this.data.customer.Pwd=e.detail.value;
    this.setData({ customer: this.data.customer});
  },
  confirmpwdCheck:function(e){
      let confirmpwd=e.detail.value;
      this.setData({confirmPwd:confirmpwd});
  },
  clearTel:function(e){//清除输入的手机号
    this.data.customer.Tel = '';
    this.setData({customer: this.data.customer});
  },
  clearPwd:function(e){//清除输入的密码
    this.data.customer.Pwd = '';
    this.setData({customer: this.data.customer});
  },
  clearCpwd:function(e){//清除输入的密码
    let confirmpwd='';
    this.setData({confirmPwd:confirmpwd});
  },
  getbackPassword:function(){
      let that=this;
      if(that.data.Tel==""){
         app.showNotify(that, "手机号不能为空！");
         return false;
      }else if (!app.validate(that.data.customer.Tel, 'phone')) {
          app.showNotify(that, "手机号不正确");
          return false;
      }else if(that.data.Code==""){
          app.showNotify(that, "验证码不能为空！");
          return false;
      }else if(that.data.Code){
          httptool.ajaxBasePost("api/Common/CheckValidateCode", { Tel: that.data.Tel,Code:that.data.Code}, function (res){
            if(!res.suc){
                app.showNotify(that, res.msg);
            }  
        });
      }else if(that.data.customer.Pwd == ""){
          app.showNotify(that, "密码不能为空！");
          return false;
      }else if(that.data.customer.Pwd.length <8){
          app.showNotify(that,"密码不得小于8位！" );
          return false;
      }else if(that.data.confirmPwd.length<8){
          app.showNotify(that, "密码不得小于8位！");
          return false;
      }else if(that.data.Pwd!=that.data.confirmPwd){
          app.showNotify(that, "密码不一致！");
          return false;
      }else if (!app.validate(that.data.customer.Pwd, 'password')) {
          app.showNotify(that, "密码不规范");
          return false;
      }
      let customer = that.data.customer;
      customer.Pwd = base64.encoder(md5.hexMD5(customer.Pwd));
      httptool.ajaxBasePost("/api/Customer/ForgetCustomerPwd",customer,function(res){
          if(res.suc){
              app.showNotify(that, "密码找回成功");
              wx.redirectTo({
                url: '/pages/login/login',
              })
          }else{
            app.showNotify(that, res.msg);
          }

      });
  },
    getCode:function () {
      if (this.data.codeBtnText =="获取验证码"){
        let self=this;
        var param = {};
        param.Tel = this.data.Tel;    
        httptool.ajaxBasePost("api/Common/GetMobileCode", param, function (d) {
          console.log(d);
          if (d.suc) {
            var times = 60;
            var timer = setInterval(function () {
              times--;
              self.setData({ codeBtnText: times + "秒后重试" });
              if (times <= 0) {
                self.setData({ codeBtnText: "获取验证码" });
                clearInterval(timer);
                times = 60;
              }
            }, 1000);
          } else {
            app.showNotify(that, res.msg);
          }
        });
      }
  },
  onLoad:function(options){
    // 页面初始化 options为页面跳转所带来的参数
  },
  onReady:function(){
    // 页面渲染完成
  },
  onShow:function(){
    // 页面显示
  },
  onHide:function(){
    // 页面隐藏
  },
  onUnload:function(){
    // 页面关闭
  }
})